/*
 * Copyright (c) 2001-2009 Ihandy(Beijing) E-Biz Tech Co.,Ltd.
 * All rights reserved.
 * 北京无线天利有限公司 版权所有
 *
 * <p>BaseActionSupport.java</p>
 *   
 * Created on Mar 17, 2011, 5:01:57 PM by Administrator
 *
 */
package com.ihandy.yuncai.base;

import java.io.IOException;
import java.io.Writer;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseActionSupport extends ActionSupport {
    private static final long serialVersionUID = 1480974503165082852L;

    public HttpServletRequest getRequest() {
	return ServletActionContext.getRequest();
    }

    public HttpServletResponse getResponse() {
	return ServletActionContext.getResponse();
    }

    public HttpSession getSession() {
	return ServletActionContext.getRequest().getSession();
    }

    /**
     * 获取页面属性
     * 
     * @return
     */
    public Map<String, String> getParam() {
	Map<String, String> map = new HashMap<String, String>();
	Enumeration en = this.getRequest().getParameterNames();
	while (en.hasMoreElements()) {
	    String name = (String) en.nextElement();
	    map.put(name, this.getRequest().getParameter(name));
	}
	return map;
    }

    /**
     * 返回数据json
     * 
     * @param response
     * @param content
     * @param type
     */
    protected void returnContext(String content) {
	this.getResponse().setContentType("json/plain;charset=UTF-8");
	Writer writer = null;
	try {
	    
	    writer = this.getResponse().getWriter();
	    writer.write(content);
	    writer.flush();
	} catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    if (writer != null) {
		try {
		    writer.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	}
    }
}
